import subprocess
import sys
import time


paths = {r"C:\Users\15328\OneDrive\桌面\files\gitee\add-bricks-and-tiles",
         r"C:\Users\15328\OneDrive\桌面\files\gitee\python-study"}
msg = ":)"
def run_command(command, path):
    try:
        re = subprocess.run(
            command,
            cwd=path,
            check=True,  # 如果命令返回非零状态码则抛出异常
            capture_output=True,  # 捕获输出
            text=True,  # 以文本形式返回（而不是字节）
            timeout=10  # 设置超时时间（秒）
        )
        return re.stdout
    except subprocess.CalledProcessError as e:
        print(f"命令执行失败 " + command)
        print(f"错误输出: {e.stderr}")
        time.sleep(100)
    except subprocess.TimeoutExpired:
        print("命令执行超时")
        time.sleep(100)


def add(path):
    command = "git add -A"
    run_command(command, path)
    print("添加成功")

def commit(path):
    command = "git commit -m \"" + msg + "\""
    run_command(command, path)
    print("提交成功")

def push(path):
    command = "git push origin master"
    run_command(command, path)
    print("成功提交到远程仓库中")

def push_path(push_path):
    command = "git status"
    result = run_command(command, push_path)
    if result.__contains__("nothing to commit"):
        print("什么都没有直接退出 " + push_path)
        time.sleep(0.5)
        return
    add(push_path)
    commit(push_path)
    push(push_path)
    time.sleep(0.5)

if __name__ == '__main__':
    for i in paths:
        print(i)
        push_path(i)